package com.andlabs.gd.objects;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;

/**
 * @author johannes
 */
public class Hero extends Character {

	//======================
	// Fields
	//======================

	//======================
	// MEMBERS
	//======================
	private Context mContext;
	private static List<Bitmap> mWalkingEast = new ArrayList<Bitmap>();
	private static List<Bitmap> mWalkingWest = new ArrayList<Bitmap>();
	private static List<Bitmap> mWalkingNorth = new ArrayList<Bitmap>();
	private static List<Bitmap> mWalkingSouth = new ArrayList<Bitmap>();
	private static List<Bitmap> mWalkingSouthEast = new ArrayList<Bitmap>();
	private static List<Bitmap> mWalkingSouthWest = new ArrayList<Bitmap>();
	private static List<Bitmap> mWalkingNorthEast = new ArrayList<Bitmap>();
	private static List<Bitmap> mWalkingNorthWest = new ArrayList<Bitmap>();
	private static List<Bitmap> mLastWalkingList;
	
	private Bitmap mDrawingBitmap;
	private float mStepTime = 0.7f;
	private int mLastPosition = 0;
	private float mStepTimePassed = 0;
	private int mSpriteWidth, mSpriteHeight;
	public int mPermanentX = 0, mPermanentY = 0;
	
	private LiveLine mLiveLine;
	//======================
	// Constructors
	//======================
	public Hero(Context context){
		mVelocity = 80;
		mContext = context;
	}
	//======================
	// Overridden Methods
	//======================
	private Bitmap mItBitmap;
	@Override
	public void render(Canvas canvas, float zoomLevel) {
		if(mPermanentX == 0 && mPermanentY == 0){
			mPermanentX = (canvas.getWidth() / 2) - (mSpriteWidth / 2);
			mPermanentY = (canvas.getHeight() / 2) - (mSpriteHeight / 2);
		}
		
		if(zoomLevel != 1){
			mItBitmap = Bitmap.createScaledBitmap(getNextBitmap(), (int) (mSpriteWidth / zoomLevel), (int) (mSpriteHeight / zoomLevel), false);
		
			mLiveLine.mSpriteHeight = mItBitmap.getHeight();
			mLiveLine.mSpriteWidth = mItBitmap.getWidth();
			
			canvas.drawBitmap(mItBitmap, mPermanentX, mPermanentY, null);

//			canvas.drawBitmap(mItBitmap, mPosition.x - (mSpriteWidth / zoomLevel / 2), mPosition.y - (mSpriteHeight /zoomLevel / 2), null);
		}else{
			canvas.drawBitmap(getNextBitmap(), mPermanentX, mPermanentY, null);

//			canvas.drawBitmap(getNextBitmap(), mPosition.x - (mSpriteWidth / 2), mPosition.y - (mSpriteHeight / 2), null);
		}
		
		mLiveLine.render(canvas, (float)mPower / (float)mMaxPower, mPermanentX , mPermanentY);
	}

	@Override
	public void update(float delta) {
		Log.d("Hero", "DestinationX = "+ mDestinationX);
		Log.d("Hero", "DestinationY = "+ mDestinationY);
		updatePosition(delta);
		
		mStepTimePassed += delta;
		while(mStepTimePassed > mStepTime){
			mStepTimePassed -= mStepTime;
		}
	}
	
	//======================
	// Public Methods
	//======================

	//======================
	// Private Methods
	//======================
	private Bitmap getNextBitmap() {
		float stepLength = mStepTime / mWalkingEast.size();
		switch(mState){
		case STATE_WALKING:
			if(mDirectionY == DIRECTION_SOUTH){
				if(mDirectionX == DIRECTION_EAST){
					mLastWalkingList = mWalkingSouthEast;
					return mWalkingSouthEast.get((int) (mStepTimePassed / stepLength));
				} else if(mDirectionX == DIRECTION_WEST){
					mLastWalkingList = mWalkingSouthWest;
					return mWalkingSouthWest.get((int) (mStepTimePassed / stepLength));
				} else{				
					mLastWalkingList = mWalkingSouth;
					return mWalkingSouth.get((int) (mStepTimePassed / stepLength));
				}
			}else if(mDirectionY == DIRECTION_NORTH){
				if(mDirectionX == DIRECTION_EAST){
					mLastWalkingList = mWalkingNorthEast;
					return mWalkingNorthEast.get((int) (mStepTimePassed / stepLength));
				} else if(mDirectionX == DIRECTION_WEST){
					mLastWalkingList = mWalkingNorthWest;
					return mWalkingNorthWest.get((int) (mStepTimePassed / stepLength));
				} else{				
					mLastWalkingList = mWalkingNorth;
					return mWalkingNorth.get((int) (mStepTimePassed / stepLength));
				}
			}
			
			if(mDirectionX == DIRECTION_WEST){
				mLastWalkingList = mWalkingWest;
				return mWalkingWest.get((int) (mStepTimePassed / stepLength));
			}else if(mDirectionX == DIRECTION_EAST){
				mLastWalkingList = mWalkingEast;
				return mWalkingEast.get((int) (mStepTimePassed / stepLength));
			}			
			
		case STATE_STOPPED:
			if(mLastWalkingList != null){
				return mLastWalkingList.get(0);
			}
			return mWalkingEast.get(0);
		}		
		return null;
	}
	
	private void initializeWalking() {
		AssetManager manager = mContext.getAssets();
		
		try {
			for(int i = 0; i <= 7; i++){
				if(mWalkingEast.size() != 7){
					mWalkingEast.add(BitmapFactory.decodeStream(manager.open("hero/walking/walking e000"+ i +".bmp")));
				}
				if(mWalkingWest.size() != 7){
				mWalkingWest.add(BitmapFactory.decodeStream(manager.open("hero/walking/walking w000"+ i +".bmp")));
				}
				if(mWalkingNorth.size() != 7){
					mWalkingNorth.add(BitmapFactory.decodeStream(manager.open("hero/walking/walking n000"+ i +".bmp")));
				}
				if(mWalkingSouth.size() != 7){
					mWalkingSouth.add(BitmapFactory.decodeStream(manager.open("hero/walking/walking s000"+ i +".bmp")));
				}
				if(mWalkingSouthEast.size() != 7){
					mWalkingSouthEast.add(BitmapFactory.decodeStream(manager.open("hero/walking/walking se000"+ i +".bmp")));
				}
				if(mWalkingSouthWest.size() != 7){
					mWalkingSouthWest.add(BitmapFactory.decodeStream(manager.open("hero/walking/walking sw000"+ i +".bmp")));
				}
				if(mWalkingNorthEast.size() != 7){
					mWalkingNorthEast.add(BitmapFactory.decodeStream(manager.open("hero/walking/walking ne000"+ i +".bmp")));
				}
				if(mWalkingNorthWest.size() != 7){
					mWalkingNorthWest.add(BitmapFactory.decodeStream(manager.open("hero/walking/walking nw000"+ i +".bmp")));					
				}
			}			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void setSpriteWidth(){
		mSpriteWidth = mWalkingEast.get(0).getWidth();
	}
	
	private void setSpriteHeight(){
		mSpriteHeight = mWalkingEast.get(0).getHeight();		
	}
	
	private void updatePosition(float delta){
		if((int) mPosition.x < mDestinationX){
			mDirectionX = DIRECTION_EAST;
		}else if((int) mPosition.x > mDestinationX){
			mDirectionX = DIRECTION_WEST;
		}else{
			mDirectionX = DIRECTION_NO;
		}
		mPosition.x += mDirectionX * delta * mVelocity;
		
		if((int) mPosition.y > mDestinationY){
			mDirectionY = DIRECTION_NORTH;
		}else if((int) mPosition.y < mDestinationY){
			mDirectionY = DIRECTION_SOUTH;
		}else{
			mDirectionY = DIRECTION_NO;
		}
		mPosition.y += mDirectionY * delta * mVelocity;
		
		if((int) mPosition.y == mDestinationY && mPosition.x == mDestinationX){
			mState = STATE_STOPPED;
		} else{
			mState = STATE_WALKING;
		}		
	}

	@Override
	public void initializeRendering(Context c) {
		initializeWalking();
		setSpriteWidth();
		setSpriteHeight();
		mLiveLine = new LiveLine(mSpriteWidth, mSpriteHeight);
	}
}

